//产品数据操作模块
//数据操作模块
var sqlHelper=require('../conf/sqlHelper');

function getGoodsTopbyNum(num,callback) {
	// body...
	var sqlstr='select * from shop_product  ORDER BY add_time DESC limit 0,?';
    var param=[num];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){   
    		callback(results);
    	}    		
    });
}

//根据产品ID查询单个详细产品信息
function getGoodsById(id,callback){
	var sqlstr='select * from shop_product where product_Id=?';
    var param=[id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){           
    		callback(results);
    	}    		
    });
}

//根据分类ID查找对应产品列表信息
function getGoodlistByCategoryId(id,callback){
	var sqlstr='select * from shop_product where category_Id=?';
    var param=[id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){
    		callback(results);
    	}    		
    });
}

//根据分类ID获取指定数量的产品信息列表
function getTopNumGoodlistByCategoryId(id,topNum,callback){
    var sqlstr='select * from shop_product where category_Id=? limit ?';
    var param=[id,topNum];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            callback(results);
        }           
    });
}


//获取产品分页信息
function getGoodsListPager(currentPage,pageSize,callback){
    //计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT * FROM shop_product LIMIT ?,?';
    var param=[startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//获取产品总行数
function getGoodsCounts(callback){
    var sqlstr='select count(*) as count from shop_product';

    sqlHelper.ExecuteReader(sqlstr,null,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//添加商品信息
function addGoods(pdInfo,callback) {
    var sqlstr='insert into shop_product(product_name,old_price,new_price,product_img,product_imglist,product_detail,product_amount,category_Id,product_favourable,product_expressfee)values(?,?,?,?,?,?,?,?,?,?)';
    var param=[pdInfo.getpdName(),pdInfo.getold_price(),pdInfo.getnew_price(),pdInfo.getproduct_img(),pdInfo.getproduct_imglist(),pdInfo.getproduct_detail(),pdInfo.getproduct_amount(),pdInfo.getcategory_Id(),pdInfo.getproduct_favourable(),pdInfo.getproduct_expressfee()];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }

    });
}

//修改编辑商品信息
function updateGoodsByID(product_Id,pdInfo,callback){
    var sqlstr='UPDATE shop_product set product_name=?,old_price=?,new_price=?,product_img=?,product_imglist=?,product_detail=?,product_amount=?,category_Id=?,product_favourable=?,product_expressfee=? where product_Id=?';
    var param=[pdInfo.getpdName(),pdInfo.getold_price(),pdInfo.getnew_price(),pdInfo.getproduct_img(),pdInfo.getproduct_imglist(),pdInfo.getproduct_detail(),pdInfo.getproduct_amount(),pdInfo.getcategory_Id(),pdInfo.getproduct_favourable(),pdInfo.getproduct_expressfee(),product_Id];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}

//分类页面加载商品信息
//根据商品分类ID和添加时间倒叙排列
function getGoodsListPagerByCateId(category_Id,currentPage,pageSize,callback) {
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT * FROM shop_product WHERE category_Id=? LIMIT ?,?';
    var param=[category_Id,startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//根据分类计算当前类别下商品的总行数
function getGoodsCountsByCateId(category_Id,callback){
    var sqlstr='select count(*) as count from shop_product where category_Id=?';
    var param=[category_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//搜索产品分页方法
//参数搜索关键字、分页参数
function searchGoodsByKey(searchKey,currentPage,pageSize,callback){
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT * FROM shop_product WHERE product_name like ? LIMIT ?,?';
    var param=['%'+searchKey+'%',startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//搜索产品分页方法
//参数搜索关键字、分页参数
function searchGoodsBySearchKey(searchKey,currentPage,pageSize){
    var startIndex=(currentPage-1)*pageSize;

    var sqlstr='SELECT * FROM shop_product WHERE product_name like ? LIMIT ?,?';
    var param=['%'+searchKey+'%',startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
           return results;
        }
    }); 
}

//根据搜索关键字查询数据总行数
function getGoodsCountsByKey(searchKey,callback){
    var sqlstr='select count(*) as count from shop_product where product_name like ?';
    var param=['%'+searchKey+'%'];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

exports.getGoodsTopbyNum=getGoodsTopbyNum;
exports.getGoodsById=getGoodsById;
exports.getGoodlistByCategoryId=getGoodlistByCategoryId;
exports.getGoodsListPager=getGoodsListPager;
exports.getGoodsCounts=getGoodsCounts;
exports.addGoods=addGoods;
exports.getGoodsListPagerByCateId=getGoodsListPagerByCateId;
exports.getGoodsCountsByCateId=getGoodsCountsByCateId;
exports.searchGoodsByKey=searchGoodsByKey;
exports.getGoodsCountsByKey=getGoodsCountsByKey;
exports.searchGoodsBySearchKey=searchGoodsBySearchKey;
exports.updateGoodsByID=updateGoodsByID;
exports.getTopNumGoodlistByCategoryId=getTopNumGoodlistByCategoryId;